package com.raising.modules.code.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.bind.annotation.*;

import com.raising.framework.controller.BaseController;
import com.raising.framework.entity.ResultVo;
import com.raising.framework.interceptor.Page;
import com.raising.modules.sys.utils.DictUtils;

import com.raising.utils.JStringUtils;

import com.raising.modules.code.entity.CodeRecord;
import com.raising.modules.code.service.CodeRecordService;

/**
 * 生成记录 控制器
 * @author gaoyuan
 * @createTime 2018-01-02 20:26:14
 */
@Controller
@RequestMapping("${adminPath}/code/codeRecord")
public class CodeRecordController extends BaseController {

    @Autowired
    private CodeRecordService codeRecordService;
    /**
     * 获取单个对象（共用）
     * @author gaoyuan
     * @createTime 2018-01-02 20:26:14
     */
    @ModelAttribute
    public CodeRecord get(@RequestParam(required = false) String id) {
        CodeRecord entity = null;
        if (JStringUtils.isNotBlank(id)) {
            entity = codeRecordService.get(id);
        }
        if (entity == null) {
            entity = new CodeRecord();
        }
        return entity;
    }
    /**
    * 跳转至列表页面
    * @author gaoyuan
    * @createTime 2018-01-02 20:26:14
    */
    @RequiresPermissions("code:codeRecord:view")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {
        return "code/codeRecord/list";
    }

    /**
    * 加载列表分页数据
    * @author gaoyuan
    * @createTime 2018-01-02 20:26:14
    */
    @RequiresPermissions("code:codeRecord:view")
    @RequestMapping(value = "/getPage.json", method = RequestMethod.POST)
    public @ResponseBody ResultVo getPage(Model model, CodeRecord codeRecord,Page<CodeRecord> page) {
        page.setEntity(codeRecord);
        page.setOrderBy(" a.id asc ");
        return new ResultVo(ResultVo.SUCCESS,"ok",codeRecordService.getPage(page));
    }


    /**
    * 跳转查看页面
    * @author gaoyuan
    * @createTime 2018-01-02 20:26:14
    */
    @RequiresPermissions("code:codeRecord:view")
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String view(CodeRecord codeRecord, Model model) {
        model.addAttribute("codeRecord", codeRecord);
        // 是否可用
        model.addAttribute("yn", DictUtils.getDictList("DIM_YN"));
        return "code/codeRecord/view";
    }

    /**
    * 执行删除操作
    * @author gaoyuan
    * @createTime 2018-01-02 20:26:14
    */
    @RequiresPermissions("code:codeRecord:delete")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(String id,int pageNo,int pageSize, RedirectAttributes redirectAttributes) {
        codeRecordService.delete(id);
        addMessage(redirectAttributes,"删除成功");
        return "redirect:"+ adminPath +"/code/codeRecord/list?pageNo="+pageNo+"&pageSize="+pageSize;
    }
}
